package com.ruoyi.web.controller.videoabalysis;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.Apps.Utils.AppUtils;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.social.domain.SocialVideo;
import com.ruoyi.social.service.ISocialVideoService;
import com.ruoyi.video.domain.VideoAnalysisUser;
import com.ruoyi.video.domain.VideoLineItem;
import com.ruoyi.video.service.IVideoAnalysisUserService;
import com.ruoyi.video.service.IVideoLineItemService;
import com.ruoyi.video.view.VideoAnalysisView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 视频解析Controller
 * 
 * @author ruoyi
 * @date 2025-03-07
 */
@RestController
@RequestMapping("/video-analysis")
public class WebVideoAnalysisController extends BaseController {
    @Autowired
    private ISocialVideoService videoService;

    @Autowired
    private IVideoAnalysisUserService videoUserService;

    @Autowired
    private IVideoLineItemService lineItemService;


    /**
     * 查询视频解析列表
     */
//    @PreAuthorize("@ss.hasPermi('video:VideoAnalysis:list')")
    @GetMapping("/list")
    public TableDataInfo list(SocialVideo socialVideo) {
        startPage();
        List<SocialVideo> list = videoService.selectSocialVideoListByUserId(SecurityUtils.getUserId(), socialVideo);
        List<VideoAnalysisView> listView = list.stream().map(VideoAnalysisView::new).collect(Collectors.toList());
        TableDataInfo res = getDataTable(list);
        res.setRows(listView);
        return res;
    }

    /**
     * 获取视频解析详细信息
     */
//    @PreAuthorize("@ss.hasPermi('video:VideoAnalysis:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        Long inId = AppUtils.getSystemInnerId(id);
        SocialVideo analysis = videoService.selectFullVideoAnalysisById(SecurityUtils.getUserId(), inId);

        VideoAnalysisView analysisView = new VideoAnalysisView(analysis);
        return success(analysisView);
    }


    @PostMapping("/public/fghijklmnop/change-status")
    public AjaxResult aiChangeStatus(@RequestBody JSONObject object) {
        return toAjax(videoService.aiChangeStatus(object));
    }

    /**
     * 修改视频解析
     */
//    @PreAuthorize("@ss.hasPermi('video:VideoAnalysis:edit')")
    @Log(title = "视频解析", businessType = BusinessType.UPDATE)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SocialVideo videoAnalysis) {
        if (StringUtils.isEmpty(videoAnalysis.getVideoLink())) {
            throw new ServiceException("请输入有效的地址");
        }
        return toAjax(videoService.addAnalysisSocialVideo(videoAnalysis));
    }

    @Log(title = "视频解析", businessType = BusinessType.UPDATE)
    @PostMapping("/reanalysis/{id}")
    public AjaxResult add(@PathVariable String id) {
        Long innerId = AppUtils.getSystemInnerId(id);
        videoService.reAnalysisSocialVideo(innerId);
        return toAjax(1);
    }

    /**
     * 删除视频解析
     */
    @PreAuthorize("@ss.hasPermi('video:VideoAnalysis:remove')")
    @Log(title = "视频解析", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{id}")
    public AjaxResult remove(@PathVariable String id) {
        Long inId = AppUtils.getSystemInnerId(id);
        return toAjax(videoUserService.deleteVideoAnalysisUserById(inId));
    }

    /**
     * 导出视频解析列表
     */
    @PreAuthorize("@ss.hasPermi('video:VideoAnalysis:export')")
    @Log(title = "视频解析", businessType = BusinessType.EXPORT)
    @PostMapping("/export/{id}")
    public void export(HttpServletResponse response, @PathVariable String id) {
        Long inId = AppUtils.getSystemInnerId(id);
        SocialVideo video = videoService.selectSocialVideoById(inId);

        List<VideoLineItem> videoLineItems = new ArrayList();
        if (StringUtils.isNotNull(video)) {
            Long userId = SecurityUtils.getUserId();
            VideoAnalysisUser user = videoUserService.selectVideoAnalysisUserById(userId, inId);
            if (StringUtils.isNotNull(user) || Objects.equals(user.getStatus(),"END")) {
                VideoLineItem search = new VideoLineItem();
                search.setUserId(userId);
                search.setVideoId(inId);
                videoLineItems = lineItemService.selectVideoLineItemList(search);
            }
        }
        ExcelUtil<VideoLineItem> util = new ExcelUtil<VideoLineItem>(VideoLineItem.class);
        util.exportExcel(response, videoLineItems, "视频解析数据");
    }
}